<!--
 * @Author: Dorange.Wang
 * @Date: 2021-11-12 10:48:01
 * @LastEditors: your name
 * @LastEditTime: 2021-11-16 10:41:21
 * @Description: file content
-->
<template>
  <el-dialog :visible="showVisible" center :show-close="false" width="800px">
    <div class="b g3 f16 tl" slot="title">
      <div class="ks-row-between pl15 pr15">
        <span>{{$t('sys.historical_records')}}</span>
        <span
          class="el-icon-close g9 f26 poi"
          @click="showVisible = false"
        ></span>
      </div>
    </div>

    <div class="pl30 pr30">
      <el-table
        ref="table"
        :data="tableData"
        style="width: 100%"
        current-row-key="id"
        stripe
        border
        v-loading="loading"
      >
        <el-table-column prop="version" label="#" width="48">
          <template slot-scope="scope">
            {{ scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="title" :label="$t('sys.record_title')">
          <template slot-scope="scope">
            <span :title="decode(scope.row.dml)">{{ scope.row.title }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="actions" :label="$t('sys.operation')" width="150">
          <template slot-scope="scope">
            <span
              class="table-actionItem"
            ><el-button
              type="text"
              @click="handleActivate(scope.row)"
            >{{$t('sys.use')}}</el-button
            ></span
            >
            <span class="table-actionItem">
              <el-popconfirm
                :title="$t('sys.delete_without_recovery')"
                placement="top"
                @confirm="handleDelete(scope.row)"
              >
                <el-button
                  class="link-danger"
                  type="text"
                  slot="reference"
                >{{$t('sys.delete')}}</el-button
                >
              </el-popconfirm>
            </span>
          </template>
        </el-table-column>
      </el-table>
      <div class="tr mt24">
        <x-pagination
          :total="total"
          :pageSize.sync="pageData.pageSize"
          :pageNo.sync="pageData.pageNo"
          @search="loadHistory()"
        />
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { getHmwebsqlPageList,deleteHmwebsql } from '@/apis/paas/HmWebSqlController.js'
import { decode } from 'js-base64'

export default {
  data () {
    return {
      loading: false,
      showVisible: false,
      pageData: {
        pageSize: 10,
        pageNo: 1
      },
      total: 0,
      tableData: [],
      options: {}
    }
  },

  methods: {
    decode,
    open (options = {}) {
      this.tableData = []
      this.pageData = {
        pageSize: 10,
        pageNo: 1
      }
      this.showVisible = true
      this.options = { ...options }
      this.loadHistory()
    },

    close () {
      this.showVisible = false
    },

    async loadHistory (pageNo = this.pageData.pageNo) {
      const res = await getHmwebsqlPageList({
        ...this.pageData,
        pageNo
      })
      this.tableData = res.data
      this.total = res.totalCount
      this.pageData = {
        pageSize: res.pageSize,
        pageNo: res.pageNo
      }
    },

    async handleActivate (row) {
      const { dml } = row
      const { onActivate } = this.options
      onActivate && onActivate(dml)
    },

    async handleDelete (row) {
      deleteHmwebsql([row.id]).then(res=>{
        this.loadHistory()
      })
    }
  }
}
</script>

<style lang="scss" scoped></style>
